﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Demo
{
    internal class SimpleSpinlock
    {
        private int m;  //0 false 1 true

        public void Enter()
        {
            while (true)
            {
                Console.WriteLine(m );
                var d = Interlocked.Exchange(ref m, 1);
                Console.WriteLine(m+"======="+d);
                if ( d== 0)
                {
                    Console.WriteLine( m+"---"+d);
                    return;
                }
            }
        }

        public void Leave()
        {
            Console.WriteLine("leave begin "+m);
            Volatile.Write(ref m,0);
            Console.WriteLine("leave end "+m);
        }
    }
}
